package com.keyancompany.zhongbaopingtai.api;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;

import com.alibaba.fastjson.JSONObject;
import com.keyancompany.zhongbaopingtai.ConstansUrl;
import com.keyancompany.zhongbaopingtai.Constants;
import com.keyancompany.zhongbaopingtai.TeamCreateActivity;
import com.keyancompany.zhongbaopingtai.ZBApplication;
import com.keyancompany.zhongbaopingtai.activity.TeamDetailActivity;
import com.keyancompany.zhongbaopingtai.listener.IAsyncHttpResponseHandler;
import com.keyancompany.zhongbaopingtai.listener.LoadingHanlder;
import com.keyancompany.zhongbaopingtai.model.LoadProjectTeam;
import com.keyancompany.zhongbaopingtai.utils.SPUtils;
import com.loopj.android.http.RequestParams;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

/**
 * ABOUT TEAM
 *
 * @author XIHE
 *         .
 */
public class TeamAPI extends BaseAPI {
//    public static final String teamCode = "2c80f6eb-d4ee-4734-9974-a5ddf621fcd7";

    public static final int TEAM_INDEX_MARK = 0X411;
    public static final int LOAD_PROJECT_TEAM_MARK = 0X412;
    public static final int LABEL_QUERY_MARK = 0X413;
    public static final int CHECK_LABEL_MARK = 0X414;
    public static final int LABEL_SAVE_MARK = 0X415;
    public static final int LOAD_WEEKLY_MARK = 0X416;
    public static final int SHOW_WEEKLY_MARK = 0X417;
    public static final int SAVE_WEEKLY_MARK = 0X418;
    public static final int SAVE_TEAM_TALK_MARK = 0X419;
    public static final int SHOW_TALK_MARK = 0X420;
    public static final int LOAD_TALK_MARK = 0X421;
    public static final int SAVE_TEAM_TALK_REPLY_MARK = 0X422;
    public static final int QUERY_TEAM_SHARE_MARK = 0X423;
    public static final int UPLOAD_MARK = 0X424;
    public static final int OVER_YES_MARK = 0X425;
    public static final int SET_LEADER_MARK = 0X426;
    public static final int EXIT_TEAM_MARK = 0X427;
    public static final int DOWNLOAD_MARK = 0X428;
    public static final int TEAM_TASK_TOME_MARK = 0X429;
    public static final int TEAM_TASK_MARK = 0X430;
    public static final int LOOK_ONE_TASK_MARK = 0X431;
    public static final int LOAD_TABLE_MARK = 0X432;
    public static final int OTHER_TEAM_MARK = 0X433;
    public static final int PROJECT_LIST_MARK = 0X434;
    public static final int ADD_PROJECT_MARK = 0X435;
    public static final int MY_PROJECT_LIST_MARK = 0X436;
    public static final int SELLER_MARK = 0X437;
    public static final int LIST_TRECEIVING = 0X438;
    public static final int TEAM_DETAIL = 0x439;
    public static final int NEW_TEAM = 0x440;
    public static final int LOAD_PROJECT = 0x441;
    public static final int SAVE_TASK = 0X442;
    public static final int OVER_PHONE = 0x443;
    public static final int FIND_STAFF = 0x444;

//    public static LoadProjectTeam getLoadTeam() {
//
//        Context context = ZBApplication.getInstance();
//        if (loadProjectTeam == null) {
//            String value = SPUtils.getComment(context).getString(Constants.LOAD_PROJECT_TEAM, null);
//            if (TextUtils.isEmpty(value)) {
//                JumpMain();
//                return null;
//            }
//            loadProjectTeam = JSONObject.parseObject(value, LoadProjectTeam.class);
//        }
//        return loadProjectTeam;
//    }
//    public static String getTeamCode() {
//        if (loadProjectTeam == null) {
//            loadProjectTeam = getLoadTeam();
//        }
//        return loadProjectTeam == null ? "" : "" + loadProjectTeam.getTeamUser().get();
//    }

//    private void getTeamCode() {
//        Context context = ZBApplication.getInstance();
//        SharedPreferences preferences = SPUtils.getComment(context);
//        SharedPreferences.Editor editor = preferences.edit();
//        // 存入数据
//        editor.putBoolean(Constants.TEAM_CODE, false);
//        // 提交修改
//        editor.commit();
//        S
//    }


    /**
     * 查询我的团队列表
     * <p/>
     * userId=xxx
     */
    public static final void teamIndex(String isprovisional, IAsyncHttpResponseHandler handler, LoadingHanlder loading) {

        RequestParams params = new RequestParams();
        params.put("userId", UserAPI.getUserId());
        params.put("isprovisional", isprovisional);
        post(ConstansUrl.TEAM_INDEX, params, handler, TEAM_INDEX_MARK, loading);
    }

    /**
     * 团队主页
     * <p/>
     * userId:用户代码
     * teamCode:团队代码
     */

    public static final void loadProjectTeam(String teamCode, IAsyncHttpResponseHandler handler, LoadingHanlder loading) {

        RequestParams params = new RequestParams();
        params.put("userId", UserAPI.getUserId());
        params.put("teamCode", teamCode);
        post(ConstansUrl.LOAD_PROJECT_TEAM, params, handler, LOAD_PROJECT_TEAM_MARK, loading);

    }

    /**
     * 我的标签
     *
     * @param :用户代码
     * @param :团队代码
     */

    public static final void labelQuery(String teamCode, IAsyncHttpResponseHandler handler, LoadingHanlder loading) {

        RequestParams params = new RequestParams();
        params.put("userId", UserAPI.getUserId());
        params.put("teamCode", teamCode);
        post(ConstansUrl.LABEL_QUERY, params, handler, LABEL_QUERY_MARK, loading);
    }

    /**
     * 标签名称校验
     *
     * @param name:标签名称
     */

    public static final void checkLabel(String name, IAsyncHttpResponseHandler handler, LoadingHanlder loading) {

        RequestParams params = new RequestParams();
        params.put("name", name);
        post(ConstansUrl.CHECK_LABEL, params, handler, CHECK_LABEL_MARK, loading);
    }

    /**
     * 标签新增
     *
     * @param :用户代码
     * @param :团队代码
     * @param labelName：标签名称
     * @param labelcolor：标签颜色
     */

    public static final void labelsave(String teamCode, String labelName, String labelcolor, IAsyncHttpResponseHandler handler, LoadingHanlder loading) {

        RequestParams params = new RequestParams();
        params.put("userId", UserAPI.getUserId());
        params.put("teamCode", teamCode);
        params.put("labelName", labelName);
        params.put("labelcolor", labelcolor);
        post(ConstansUrl.LABEL_SAVE, params, handler, LABEL_SAVE_MARK, loading);
    }

    /**
     * 周报列表
     *
     * @param :用户代码
     * @param :团队代码
     * @param currentPage:分页页码
     */

    public static final void loadWeekly(String teamCode, String currentPage, IAsyncHttpResponseHandler handler, LoadingHanlder loading) {

        RequestParams params = new RequestParams();
        params.put("userId", UserAPI.getUserId());
        params.put("teamCode", teamCode);
        params.put("pagenombers", 1 + "");

        post(ConstansUrl.LOAD_WEEKLY, params, handler, LOAD_WEEKLY_MARK, loading);
    }

    /**
     * 查看周报
     *
     * @param :用户代码
     * @param :团队代码
     * @param weeklyId:周报Id
     */

    public static final void showWeekly(String teamCode, String weeklyId,
                                        IAsyncHttpResponseHandler handler, LoadingHanlder loading) {

        RequestParams params = new RequestParams();
        params.put("userId", UserAPI.getUserId());
        params.put("teamCode", teamCode);
        params.put("weeklyId", weeklyId);
        post(ConstansUrl.SHOW_WEEKLY, params, handler, SHOW_WEEKLY_MARK, loading);
    }

    /**
     * 发表周报
     *
     * @param :用户代码
     * @param :团队代码
     * @param weeklyCont：周报内容
     * @param weekDate:周报年份
     */

    public static final void saveWeekly(String teamCode, String weeklyCont,
                                        String weekDate, IAsyncHttpResponseHandler handler,
                                        LoadingHanlder loading) {

        RequestParams params = new RequestParams();
        params.put("userId", UserAPI.getUserId());
        params.put("teamCode", teamCode);
        params.put("weekCont", weeklyCont);
        params.put("weekDate", weekDate);
        post(ConstansUrl.SAVE_WEEKLY, params, handler, SAVE_WEEKLY_MARK, loading);
    }

    /**
     * 发表讨论
     *
     * @param :用户代码
     * @param :团队代码
     * @param talkTitle：讨论标题
     * @param weekCont:讨论内容
     * @param pjttId:讨论Id（空为新增，有值 则为修改）
     */

    public static final void saveTeamTalk(String teamCode, String talkTitle,
                                          String weekCont, String pjttId, IAsyncHttpResponseHandler handler, LoadingHanlder loading) {

        RequestParams params = new RequestParams();
        params.put("userId", UserAPI.getUserId());
        params.put("teamCode", teamCode);
        params.put("talkTitle", talkTitle);
        params.put("weekCont", weekCont);
        params.put("pjttId", pjttId);
        post(ConstansUrl.SAVE_TEAM_TALK, params, handler, SAVE_TEAM_TALK_MARK, loading);
    }

    /**
     * 查看讨论信息
     *
     * @param :用户代码
     * @param :团队代码
     * @param talkId：讨论Id
     */

    public static final void showTalk(String teamCode, String talkId, IAsyncHttpResponseHandler handler, LoadingHanlder loading) {

        RequestParams params = new RequestParams();
        params.put("userId", UserAPI.getUserId());
        params.put("teamCode", teamCode);
        params.put("talkId", talkId);
        post(ConstansUrl.SHOW_TALK, params, handler, SHOW_TALK_MARK, loading);
    }

    /**
     * 查询讨论列表
     *
     * @param :用户代码
     * @param :团队代码
     * @param pagenombers：分页页码
     */

    public static final void loadTalk(String teamCode, int pagenombers, IAsyncHttpResponseHandler handler, LoadingHanlder loading) {

        RequestParams params = new RequestParams();
        params.put("userId", UserAPI.getUserId());
        params.put("teamCode", teamCode);
        params.put("pagenombers", pagenombers + "");
        post(ConstansUrl.LOAD_TALK, params, handler, LOAD_TALK_MARK, loading);
    }

    /**
     * 保存评论信息
     *
     * @param :用户代码
     * @param :团队代码
     * @param weekCont：评论内容
     * @param talkId：讨论ID
     */

    public static final void saveTeamTalkReply(String teamCode, String weekCont, String talkId,
                                               IAsyncHttpResponseHandler handler, LoadingHanlder loading) {

        RequestParams params = new RequestParams();
        params.put("userId", UserAPI.getUserId());
        params.put("teamCode", teamCode);
        params.put("weekCont", weekCont + "");
        params.put("talkId", talkId);
        post(ConstansUrl.SAVE_TEAM_TALK_REPLY, params, handler, SAVE_TEAM_TALK_REPLY_MARK, loading);
    }

    /**
     * 查询分享列表
     *
     * @param :用户代码
     * @param :团队代码
     * @param filePid：分享文件 Id（可以有可以没有）
     * @param talkId：讨论ID
     */

    public static final void queryTeamShare(String teamCode, String filePid, String talkId, IAsyncHttpResponseHandler handler, LoadingHanlder loading) {

        RequestParams params = new RequestParams();
        params.put("userId", UserAPI.getUserId());
        params.put("teamCode", teamCode);
        params.put("filePid", filePid);
        params.put("talkId", talkId);
        post(ConstansUrl.QUERY_TEAM_SHARE, params, handler, QUERY_TEAM_SHARE_MARK, loading);
    }

    /**
     * 团队转让其他成员列表
     *
     * @param :用户代码
     * @param teamCode:团队代码
     */

    public static final void otherTeam(String teamCode, IAsyncHttpResponseHandler handler, LoadingHanlder loading) {

        RequestParams params = new RequestParams();
        params.put("userId", UserAPI.getUserId());
        params.put("teamCode", teamCode);
        post(ConstansUrl.OTHER_TEAM, params, handler, OTHER_TEAM_MARK, loading);
    }


    /**
     * 验证码校验     成功：解散团队     失败：不解散团队
     *
     * @param :用户代码
     * @param :团队代码
     * @param code:验证码
     */

    public static final void overYes(String teamCode, String code, IAsyncHttpResponseHandler handler, LoadingHanlder loading) {

        RequestParams params = new RequestParams();
        params.put("userId", UserAPI.getUserId());
        params.put("teamCode", teamCode);
        params.put("code", code);
        post(ConstansUrl.OVER_YES, params, handler, OVER_YES_MARK, loading);
    }

    /**
     * 转让团队
     *
     * @param :用户代码
     * @param :团队代码
     * @param teamId:团队成员表记录的ID（被转让的成员在成员记录表中的ID）
     */

    public static final void setLeader(String userCode, String teamCode, String teamId, IAsyncHttpResponseHandler handler, LoadingHanlder loading) {

        RequestParams params = new RequestParams();
        params.put("userId", userCode);
        params.put("teamCode", teamCode);
        params.put("teamId", teamId);
        post(ConstansUrl.SET_LEADER, params, handler, SET_LEADER_MARK, loading);
    }

    /**
     * 退出团队
     *
     * @param :用户代码
     * @param :团队代码
     * @param teamId:团队成员表记录的ID（被转让的成员在成员记录表中的ID）
     */

    public static final void exiTeam(String teamCode, String teamId, IAsyncHttpResponseHandler handler, LoadingHanlder loading) {

        RequestParams params = new RequestParams();
        params.put("userId", UserAPI.getUserId());
        params.put("teamCode", teamCode);
        params.put("teamId", teamId);
        post(ConstansUrl.EXIT_TEAM, params, handler, EXIT_TEAM_MARK, loading);
    }

    /**
     * 文件下载
     *
     * @param :用户代码
     * @param pid:文件PID
     */

    public static final void download(String pid, IAsyncHttpResponseHandler handler, LoadingHanlder loading) {

        RequestParams params = new RequestParams();
        params.put("userId", UserAPI.getUserId());
        params.put("pid", pid);
        post(ConstansUrl.DOWNLOAD, params, handler, DOWNLOAD_MARK, loading);
    }

    /**
     * 我的任务
     *
     * @param :用户代码
     * @param :团队代码
     * @param isCreateMe :是否我发布的（0：否1：是）
     * @param pagenumber :页码
     * @param loading
     */

    public static final void teamTaskToMe(String teamCode, int pagenumber, int isCreateMe, IAsyncHttpResponseHandler handler, LoadingHanlder loading) {

        RequestParams params = new RequestParams();
        params.put("userId", UserAPI.getUserId());
        params.put("teamCode", teamCode);
        params.put("isCreateMe", isCreateMe + "");
        params.put("pagenumber", "" + pagenumber);
        params.put("pagesize", "" + DETAULT_COUNT);
        post(ConstansUrl.TEAM_TASK_TOME, params, handler, TEAM_TASK_TOME_MARK, loading);
    }

    /**
     * 团队任务（任务看板）
     *
     * @param :用户代码
     * @param :团队代码
     * @param pagenumber :页码
     * @param :每页显示数量
     * @param loading
     */

    public static final void teamTask(String teamCode, int state, int pagenumber, IAsyncHttpResponseHandler handler, LoadingHanlder loading) {

        RequestParams params = new RequestParams();
        params.put("userId", UserAPI.getUserId());
        params.put("teamCode", teamCode);
        params.put("pagenumber", pagenumber + "");
        params.put("state", "" + state);
        params.put("pagesize", "" + DETAULT_COUNT);
        post(ConstansUrl.TEAM_TASK, params, handler, TEAM_TASK_MARK, loading);
    }

    public static final void loadProject(String pid, IAsyncHttpResponseHandler handler, LoadingHanlder loading) {
        RequestParams params = new RequestParams();
        params.put("userId", UserAPI.getUserId());
        params.put("pid", pid);
        post(ConstansUrl.LOAD_PROJECT, params, handler, LOAD_PROJECT, loading);
    }

    /**
     * 查看任务
     *
     * @param :用户代码
     * @param :团队代码
     * @param taskId  :任务ID
     * @param loading
     */

    public static final void lookTask(String teamCode, String taskId, IAsyncHttpResponseHandler handler, LoadingHanlder loading) {

        RequestParams params = new RequestParams();
        params.put("userId", UserAPI.getUserId());
        params.put("teamCode", teamCode);
        params.put("taskId", taskId);
        post(ConstansUrl.LOOK_ONE_TASK, params, handler, LOOK_ONE_TASK_MARK, loading);
    }

    /**
     * 项目进度
     *
     * @param :用户代码
     * @param :团队代码
     */

    public static final void loadTable(String teamCode, IAsyncHttpResponseHandler handler, LoadingHanlder loading) {
        RequestParams params = new RequestParams();
        params.put("userId", UserAPI.getUserId());
        params.put("teamCode", teamCode);
        post(ConstansUrl.LOAD_TABLE, params, handler, LOAD_TABLE_MARK, loading);
    }
// =====项目相关

    /**
     * 项目列表接口
     *
     * @param project    学术分类
     * @param technology 中标类型
     * @param budget     预算
     * @param conditions 更多筛选条件   (0:全部 1:竞标中 2:执行中 3:已完成)
     * @param current    当前页
     * @param handler
     * @param loading
     */
    public static final void projectList(String project, String technology,
                                         String budget, String conditions,
                                         String current,
                                         IAsyncHttpResponseHandler handler,
                                         LoadingHanlder loading) {

        RequestParams params = new RequestParams();
//        params.put("project", project);
//        params.put("technology", technology);
//        params.put("budget", budget);
//        params.put("conditions", conditions);
        params.put("userId", UserAPI.getUserId());
        params.put("current", current);
        params.put("pageSize", "3");

        post(ConstansUrl.PROJECT_LIST, params, handler, PROJECT_LIST_MARK, loading);
    }


    /**
     * 我参与的项目列表接口
     *
     * @param project    学术分类
     * @param technology 中标类型
     * @param budget     预算
     * @param conditions 更多筛选条件   (0:全部 1:竞标中 2:执行中 3:已完成)
     * @param current    当前页
     * @param handler
     * @param loading
     */
    public static final void projectListTreceving(String project, String technology,
                                                  String budget, String conditions,
                                                  String current,
                                                  IAsyncHttpResponseHandler handler,
                                                  LoadingHanlder loading) {

        RequestParams params = new RequestParams();
//        params.put("project", project);
//        params.put("technology", technology);
//        params.put("budget", budget);
//        params.put("conditions", conditions);
        params.put("userId", UserAPI.getUserId());
        params.put("current", current);
        params.put("pageSize", DETAULT_COUNT + "");

        post(ConstansUrl.LIST_TRECEIVING, params, handler, LIST_TRECEIVING, loading);
    }


    /**
     * 发布项目信息
     *
     * @param
     * @param pid
     * @param project_type  项目类型：0私有项目，1公共项目
     * @param project_title 标题
     * @param project_phone 手机号
     * @param technology    中标类型
     * @param handler
     * @param loading
     */
    public static final void addProject(String pid, String project_type, String project_title, String project_phone,
                                        String technology, String project_describe, String endTime,
                                        String budget, String fenshu, String seachfenlei1, String seachfenlei11, String seachfenlei111, IAsyncHttpResponseHandler handler, LoadingHanlder loading) {

        RequestParams params = new RequestParams();
        params.put("userId", UserAPI.getUserId());
        if (!TextUtils.isEmpty(pid))
            params.put("pid", pid);
        params.put("project_type", project_type);
        params.put("project_title", project_title);
        params.put("project_phone", project_phone);
        params.put("project_endtime", endTime);
        params.put("technology", technology);
        params.put("project_describe", project_describe);
        params.put("project_budget", budget);
        params.put("project_fenshu", fenshu);
        if (!TextUtils.isEmpty(seachfenlei1))
            params.put("seachfenlei1", seachfenlei1);
        if (!TextUtils.isEmpty(seachfenlei11))
            params.put("seachfenlei11", seachfenlei11);
        if (!TextUtils.isEmpty(seachfenlei111))
            params.put("seachfenlei111", seachfenlei111);
        post(ConstansUrl.ADD_PROJECT, params, handler, ADD_PROJECT_MARK, loading);
    }


    /**
     * 我的项目
     *
     * @param
     * @param project
     * @param technology
     * @param budget
     * @param conditions
     * @param current
     * @param handler
     * @param loading
     */

    public static final void myProjectList(String project, String technology, String budget, String conditions, String current, IAsyncHttpResponseHandler handler, LoadingHanlder loading) {

        RequestParams params = new RequestParams();
        params.put("userId", UserAPI.getUserId());
        params.put("project", project);
        params.put("technology", technology);
        params.put("budget", budget);
        params.put("conditions", conditions);
        params.put("current", current);

        post(ConstansUrl.MY_PROJECT_LIST, params, handler, MY_PROJECT_LIST_MARK, loading);
    }

    /**
     * 找人列表
     *
     * @param project
     * @param city
     * @param conditions
     * @param current
     * @param handler
     * @param loading
     */
    public static final void seller(String project, String city, String conditions, String current, IAsyncHttpResponseHandler handler, LoadingHanlder loading) {

        RequestParams params = new RequestParams();
        if (!TextUtils.isEmpty(project))
            params.put("project", project);
        if (!TextUtils.isEmpty(city))
            params.put("city", city);
        if (!TextUtils.isEmpty(conditions))
            params.put("conditions", conditions);
        params.put("current", current);

        post(ConstansUrl.SELLER, params, handler, SELLER_MARK, loading);
    }

    public static final void getTeamAllInfo(String pid, IAsyncHttpResponseHandler handler, LoadingHanlder loading) {
        RequestParams params = new RequestParams();
//        params.put("userId", UserAPI.getUserId());
        params.put("pid", pid);
        post(ConstansUrl.TEAM_DETAIL, params, handler, TEAM_DETAIL, loading);
    }

    public static void newTeam(String name, IAsyncHttpResponseHandler handler, LoadingHanlder loading) {
        RequestParams params = new RequestParams();
//        params.put("userId", UserAPI.getUserId());
        params.put("userId", UserAPI.getUserId());
        params.put("teamName", name);
        post(ConstansUrl.NEW_TEAM, params, handler, NEW_TEAM, loading);
    }

    /**
     * userId:用户代码
     * teamCode:团队代码
     * title：任务标题
     * taskDis:任务描述
     * toUser:任务指派人
     * priority:优先级(0无1严重2主要3次要4不重要)
     * fromUser：创建人
     * taskStart：计划开始时间（yyyy-MM-dd HH:mm:ss）
     * taskEnd：计划结束时间（yyyy-MM-dd HH:mm:ss）
     * tasklabel：任务标签pid
     */
    public static void saveTask(String teamCode, String title, String taskDis, String toUser, String priority
            , String fromUser, String taskStart, String taskEnd, String tasklabel, IAsyncHttpResponseHandler handler, LoadingHanlder loading) {
        RequestParams params = new RequestParams();
//        params.put("userId", UserAPI.getUserId());
        params.put("userId", UserAPI.getUserId());
        params.put("teamCode", teamCode);
        params.put("title", title);
        params.put("taskDis", taskDis);
        params.put("toUser", toUser);
        params.put("priority", priority);
        params.put("fromUser", fromUser);
        params.put("taskStart", taskStart);
        params.put("taskEnd", taskEnd);
        params.put("tasklabel", tasklabel);
        post(ConstansUrl.SAVE_TASK, params, handler, SAVE_TASK, loading);
    }

    public static void overPhone(IAsyncHttpResponseHandler handler, LoadingHanlder loading) {
        RequestParams params = new RequestParams();
        params.put("userPid", UserAPI.getUserId());
        post(ConstansUrl.OVER_PHONE, params, handler, OVER_PHONE, loading);
    }

    public static void findstaff(String teamCode, String search, IAsyncHttpResponseHandler handler, LoadingHanlder loading) {
        RequestParams params = new RequestParams();
        params.put("userPid", UserAPI.getUserId());
        params.put("search", search);
        post(ConstansUrl.FIND_STAFF, params, handler, FIND_STAFF, loading);
    }
}
